package com.zxw.common.utils;

import com.zxw.common.entity.baidu.BaiduHotVO;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HotUtil {

    /**
     * 抓取百度热点排行榜
     *
     * @return
     */
    public static List<BaiduHotVO> grabBaiduHotNewsJson() {
        String url = "https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065";
        List<BaiduHotVO> list = new ArrayList<>();
        try {
            Document doc = Jsoup.connect(url).get();
            //标题
            Elements titles = doc.select(".c-single-text-ellipsis");
            //图片
            Elements imgs = doc.select(".category-wrap_iQLoo .index_1Ew5p").next("img");
            //内容
            Elements contents = doc.select(".hot-desc_1m_jR.large_nSuFU");
            //推荐图
            Elements urls = doc.select(".category-wrap_iQLoo a.img-wrapper_29V76");
            //热搜指数
            Elements levels = doc.select(".hot-index_1Bl1a");
            for (int i = 0; i < levels.size(); i++) {
                BaiduHotVO o = new BaiduHotVO();
                o.setTitle(titles.get(i).text().trim());
                o.setImg(imgs.get(i).attr("src"));
                o.setContent(contents.get(i).text().replaceAll("查看更多>", "").trim());
                o.setUrl(urls.get(i).attr("href"));
                o.setLevel(levels.get(i).text().trim());
                list.add(o);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
